import { EVector2, EVector3 } from "../../engine/Math";
import { MapAStar } from "./MapAStar";

/**
 * 正方形地图，
 */
export class RectMapAStar extends MapAStar {
    positionToCoord(position: EVector3, out?: EVector2): EVector2 {
        return this.positionToCoord2(position.x, position.z, out);
    }
    /**
     * 像素坐标转格子坐标
     * @param position 
     */
    positionToCoord2(x: number, y: number, out?: EVector2): EVector2 {
        let tileESize = this._gridESize;
        let mapESizeH = this._grid.numRows;
        let contentESize = this._mapContent;

        let startPosX = 0//mapESizeH * (tileESize.width / 2);
        let startPosY = 0//contentESize.height;

        this.tilePosDiv.x = Math.floor((x - startPosX) / tileESize.width);
        this.tilePosDiv.y = Math.floor((y - startPosY) / tileESize.height);
        let posX = this.tilePosDiv.x;
        let posY = this.tilePosDiv.y;
        // let posX = Math.floor(this.tilePosDiv.x - this.tilePosDiv.y);
        // let posY = Math.floor(-this.tilePosDiv.x - this.tilePosDiv.y);
        if (out) {
            out.set(posX, posY);
        } else {
            out = new EVector2(posX, posY);
        }
        return out;
    }

    /**
     * 格子坐标转像素坐标
     * @param tileCoord 
     * @param out 
     * @returns 
     */
    coordToPosition(tileCoord: EVector2, out?: EVector3): EVector3 {
        let tileESize = this._gridESize;
        let mapESizeH = this._grid.numRows;
        let contentESize = this._mapContent;

        // let startPosX = mapESizeH * (tileESize.width / 2);
        // let startPosY = contentESize.height;
        // this.tilePosDiv.x = (tileCoord.x - tileCoord.y) / 2;
        // this.tilePosDiv.y = (-tileCoord.x - tileCoord.y) / 2;
        let x = tileESize.width / 2 + tileCoord.x * tileESize.width//  + startPosX;
        let z = tileESize.height / 2 + tileCoord.y * tileESize.height//  + startPosX;
        if (out) {
            out.set(x, 0, z);
        } else {
            out = new EVector3(x, 0, z);
        }
        return out;
    }
}